<template>
<div class="page add-member">
    <div class="weui-cells">
        <!-- <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">小组名称</label></div>
            <div class="weui-cell__bd">
                <input v-model='item.name' class="weui-input" type="text" maxlength="20" placeholder="必填">
            </div>
        </div> -->

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">部门名称</label></div>
            <div class="weui-cell__bd">
                <input v-model="item.name" class="weui-input" type="text" placeholder="必填" maxlength="20">
            </div>
        </div>

       <!--  <a @click="selectParentGroup" class="weui-cell weui-cell_access">
            <div class="weui-cell__bd">
                <p>上级小组</p>
            </div>
            <div :value="item.parentId" class="weui-cell__ft">{{item.parentName}}</div>
        </a> -->

        <a @click="selectParentGroup" class="weui-cell weui-cell_access">
            <div class="weui-cell__bd">
                <p class="info"><span :value="item.parentId" class="title">上级部门</span>{{item.parentName}}</p>
            </div>
            <div class="weui-cell__ft">
            </div>
        </a>


    </div>
</div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import $ from 'zepto'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache'
    import baseApi from 'gapi/base'
    import clientInfo from 'gapi/clientInfo'
    import widget from 'gapi/widget'
    import commonPage from 'clientApi/commonPage.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                item: {
                    name: '',
                    parentId:'',
                    parentName:'',
                }
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.teamId = util.getUrddataString('tId')
                this.projectId = util.getUrddataString('pId')
                this.groupId = util.getUrddataString('gId')
                this.groupName = util.getUrddataString('groupName')
                this.item.parentId = this.groupId || ''
                this.item.parentName = this.groupName || ''
            },
            //选择上级小组
            selectParentGroup (){
              cache.setMemoryCache('KqCacheMessge', {
                project_id: this.projectId,
                team_id: this.teamId,
              })
              location.href = 'dmsaas://GroupSelectSingle?' + encodeURIComponent(`group_id=${this.groupId}`)
            },
            //更新上级小组
            updateData (data){
                // alert(JSON.stringify(data))
                let key = data.key
                let value = data.value
                switch(key){
                    case 'selectGroup':
                        this.item.parentId = value.id
                        this.item.parentName = value.name
                }
            },
            //提交
            doSubmit (){
                //校验
                if(!this.item.name){
                    widget.toast('小组名称是必填项')
                    return false
                }else if (!this.item.parentId){
                    widget.toast('请选择上级小组')
                    return false
                }
                util.ajax({
                    url:'/group/create',
                    type:'POST',
                    data:{
                        group_name: this.item.name,
                        team_id: this.teamId,
                        project_id: this.projectId,
                        parent_id : this.item.parentId,
                    },
                    timeout:10000,
                    beforeSend: () => {

                    },
                    success:(result) => {
                        console.log(result)
                        if(result.errno == 0){
                          widget.toast('添加小组成功')
                          // commonPage.goBack(1,{'key':'addGroup','value':''})
                          // let encodeUrl = `team_id=${this.teamId}&project_id=${this.projectId}`
                            commonPage.goBack(1)
                          ///window.location.href="dmsaas://ProjectMember?"+encodeURIComponent(encodeUrl)
                        }else{
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        // error
                        var toastStr = ''
                        if(status == 'timeout'){
                          toastStr = '网络请求超时'
                        }else{
                          toastStr = '服务器异常'
                        }

                        widget.toast(toastStr)
                    },
                    complete: () => {

                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
              }
        },
        created(){
            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('webViewID',{'webViewID':data})
            })
            this.init();
            window.loadData = this.updateData
            window.doSubmit = this.doSubmit

            //控制header
            commonPage.updateTitleBar({
                leftDisplay: false,
                rightDisplay: true,
                rightText: '完成',
                rightAction: 'doSubmit'
            })
        },
    }


</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src='../../static/css/common.css'></style>
<style src='../../static/css/user-list.css'></style>
